package com.tuo51.childcare.controller;


import com.tuo51.childcare.domain.feed.Feeds;
import com.tuo51.childcare.service.FeedsService;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.evaluate.service.ChildrenService;
import com.tuo51.evaluate.service.UploadService;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.query.base.PagerQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;


/**
 * @Describle This Class Is
 * @Author ZengMin
 * @Date 2018/11/7 10:08
 * @Company Matt
 */
@RequestMapping("/papi/feed")
@RestController
@Api(value = "/papi/familygame", tags = "家长服务号 - 时光轴")
public class PAPIFeedController {

    @Autowired
    FeedsService feedsService;

    @Autowired
    ChildrenService childrenService;

    @Autowired
    MemVerificationCache cache;

    @Autowired
    UploadService uploadService;

    @RequestMapping(value = "/getFeedList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "时光轴列表", notes = "可能混合多个测评报告对象类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页下标"),
            @ApiImplicitParam(name = "pageSize", value = "页容量"),
            @ApiImplicitParam(name = "filter", value = "类型过滤器，竖线分割")
    })
    public StatusResponse getFeedList(HttpServletRequest req,
                                      HttpServletResponse resp,
                                      @RequestHeader(value = "ticket", required = false) String ticket,
                                      String filter,
                                      PagerQuery pagerQuery) {
        //先取此用户基本信息
        String uid = cache.getStaffId(ticket);

        return StatusResponse.success(feedsService.getAllByChildrens(uid, filter, pagerQuery));
    }

    @RequestMapping(value = "/link_detail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取用户关联所有宝宝的时光轴条目关联对象详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "feedId", value = "条目 id"),
            @ApiImplicitParam(name = "pageNum", value = "分页下标"),
            @ApiImplicitParam(name = "pageSize", value = "页容量")
    })
    public StatusResponse linkDetail(@RequestHeader(value = "ticket", required = false) String ticket,HttpServletRequest req,
                                     HttpServletResponse resp,
                                     String feedId,
                                     PagerQuery pagerQuery){
        //先取此用户基本信息
        String uid = cache.getStaffId(ticket);

        //
        Feeds feedDetail = feedsService.getOne(uid, feedId);

        //
        Object linkedObjects = feedsService.getLinkedObjects(uid, feedDetail, pagerQuery);

        Map result = new HashMap();
        result.put("linkedObjects", linkedObjects);
        result.put("feed", feedDetail);

        return StatusResponse.success(result);
    }

    // 发布班级圈
    @RequestMapping(value = "/post", method = RequestMethod.POST, headers="content-type=multipart/form-data")
    @ApiOperation("家长发时光轴")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题"),
            @ApiImplicitParam(name = "content", value = "内容"),
            @ApiImplicitParam(name = "mediaType", value = "媒体类型，0图片，1视频"),
    })
    @ResponseBody
    public StatusResponse postFeed(@RequestHeader(TICKET) String ticket, String title, String content,
                                   String fileUrls,
                                   Integer mediaType) throws IOException {

        //先取此用户基本信息
        Staff parent = cache.getStaff(ticket);

        feedsService.postFeeds(parent, null, title, content, fileUrls, mediaType, FeedsService.FeedsType.FeedParentMemo, null);

        return StatusResponse.success(null);
    }
}
